package com.srd.phone.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.shengrongdai.srd.R;
import com.srd.phone.mvp.presenters.UtilPresenter;
import com.srd.phone.utils.Constants;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 充值 Activity
 */
public class RechargeActivity extends BaseActivity implements View.OnClickListener {


    private TextView tv_account_money, tv_can_money, tv_can_money_sys;
    private EditText et_money;
    private Button btn_recharge;
    private UtilPresenter utilPresenter = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);

        initView();
        initData();
    }

    public void initView() {
        super.initView();

        tv_common_title.setText("充值");
        tv_account_money = (TextView)findViewById(R.id.tv_account_money);  // 账户总额
        tv_can_money = (TextView)findViewById(R.id.tv_can_money);  // 可用金额
        tv_can_money_sys = (TextView)findViewById(R.id.tv_can_money_sys);  // 可用现金额
        et_money = (EditText)findViewById(R.id.et_money);  // 输入要充值的金额

        btn_recharge = (Button)findViewById(R.id.btn_recharge);
        btn_recharge.setOnClickListener(this);
    }

    public void initData() {
        super.initData();

        tv_account_money.setText(formatText("账户总额：￥0.00"));
        tv_can_money.setText(formatText("可提现金额：￥0.00"));

        utilPresenter = new UtilPresenter(requestQueue, this);
        presenter = utilPresenter;
        setUrlArrs(Constants.RECHARGE_APP);

    }

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        if (Constants.RECHARGE_APP.equals(tag)) {
            try {
                JSONObject jsonObj = new JSONObject(data);
                String url = jsonObj.optString("data");
                if(!TextUtils.isEmpty(url)){
                    Intent intent = new Intent(RechargeActivity.this, WebViewActivity.class);
                    intent.putExtra("urlValue", url);
                    intent.putExtra("titleName", "充值");
                    gotoActivity(intent);
                    finish();
                }else{
                    toastShow("获取url地址失败~");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onError(String tag, VolleyError error) {
        super.onError(tag, error);
        toastShow("获取url地址失败~");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btn_recharge:  // 确认充值
//                startActivity(new Intent(this, RechargeFailActivity.class));
//                startActivity(new Intent(this, RechargeSucceedActivity.class));

                String money = et_money.getText().toString().trim();
                if(TextUtils.isEmpty(money)){
                    toastShow("充值金额不能为空~");
                    return;
                }

                utilPresenter.singleParam(Constants.RECHARGE_APP, "money", money);
                break;
        }
    }

    private SpannableStringBuilder formatText(String text){
        SpannableStringBuilder style=new SpannableStringBuilder(text);
        style.setSpan(new ForegroundColorSpan(Color.parseColor("#CB484B")), text.indexOf("￥") , text.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        return style;
    }

}
